<template>
    <Table stripe :columns="columns" :data="data"></Table>
</template>
<script>
    export default {
        data () {
            return {
                columns: [
                    {
                        title: '标题',
                        key: 'title'
                    },
                    {
                        title: '分类',
                        key: 'tags',
                        sortable: true
                    },
                    {
                        title: '阅读量',
                        key: 'readingAmount',
                        sortable: true
                    },
                    {
                        title: '发布日期',
                        key: 'publishTime',
                        sortable: true
                    },
                    {
                    	title:'操作',
                    	key:'readingAmount',
                    	render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'success',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            
                                        	this.$router.push(this.$route.path+'/'+params.row._id);
                                        }
                                    }
                                }, '查看'),
                                h('Button', {
                                    props: {
                                        type: 'info',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.$router.push(this.$route.path+'/modify/'+params.row._id);
                                        }
                                    }
                                }, '修改'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                        	this.$Modal.confirm({
							                    title: '确认删除',
							                    content: '<p>删除 <strong>'+params.row.title+'</strong> ?</p>',
							                    loading: true,
							                    onOk: () => {
							                    	const that = this;
							                        this.$http.get(
												        	that.surl+'deletearticle?sessionID='+that.$cookie.get('sessionID')+'&_id='+params.row._id
												        )
											            .then(function (response) {
											          		if(response.data===true){
																that.$Modal.remove();
                                                                console.log(that.data[params.index].title);
                                                                console.log(that.data);
                                                                that.data.splice(params.index,1);
							                            		that.$Message.success('删除成功');
											          		}else{
																that.$Modal.remove();
							                            		that.$Message.error('删除失败');
											          		}
											            })
												        .catch(function (error) {
												            that.$Modal.remove();
							                            	that.$Message.error('删除失败');
												        });
												}
							                });
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }                    
                ],
                data: [

                ]
            }
        },
        mounted(){
            if(this.$cookie.get('sessionID')){
                this.$Loading.start();//加载动画开始
                const that = this;
                this.$http.get(
                that.surl+'home?sessionID='+that.$cookie.get('sessionID')
            )
          .then(function (response) {
            that.$Loading.finish();//加载完毕
            if(response.data!==false){
              that.data=response.data;
            }else{
                that.$router.push('/admin/404');
            }
          })
          .catch(function (error) {
            that.$router.push('/admin/404');
          });
    }
        }
    }
</script>